package ext.tianma.project2.APQP.queue;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.scheduler.ScheduleItem;
import wt.scheduler.SchedulingHelper;
import wt.util.WTException;
import ext.tianma.util.UserUtil;

/**
 * @author Elead
 */
public class CreateQueue implements RemoteAccess{

	private static final Logger logger = LoggerFactory.getLogger(CreateQueue.class);
    //执行周期时间为1天
    private static final long PERIOD_DAY = 24 * 60 * 60;
    /**
     * 创建计划执行队列，只需要执行一次即可，以后当windchill服务启动则按照设定的时间定时执行任务
     *
     * @throws WTException
     */
    public static void createScheduleQueue() throws WTException {
    	
    	if (!RemoteMethodServer.ServerFlag) {
			try {
				Class[] argTypes = {};
				Object[] args = {};
				RemoteMethodServer.getDefault().invoke("createScheduleQueue", CreateQueue.class.getName(), null, argTypes, args);
			} catch (RemoteException rme) {
				rme.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} else {
			//创建计划队列对象
	        ScheduleItem scheduleItem = ScheduleItem.newScheduleItem();
	        //设置该队列的描述信息
	        scheduleItem.setItemDescription("");
	        //设置队列的名称，一般以英文命名
	        scheduleItem.setQueueName("APQPScheduleItem");
	        scheduleItem.setItemName("changeActivityState");
	        //设置要执行的类和方法
	        scheduleItem.setTargetClass(RunQueue.class.getName());
	        //方法名
	        scheduleItem.setTargetMethod("runScheduleQueue");
	        scheduleItem.setToBeRun(-1L);

	        //设置开始时间为每天的 01:00:00
	        Calendar calendar = Calendar.getInstance();
	        calendar.set(Calendar.HOUR_OF_DAY, -7); //小时设置为-7，考虑时差，则为凌晨1点，
	        calendar.set(Calendar.MINUTE, 0);
	        calendar.set(Calendar.SECOND, 0);
	        Date today = calendar.getTime();

	        //当前时间大于设定的启动时间，天数+1
	        if (today.before(new Date())) {
	           today = addDay(today,1);
	        }
	        Timestamp timestamp = new java.sql.Timestamp(today.getTime());

	        scheduleItem.setStartDate(timestamp);
	        scheduleItem.setPeriodicity(PERIOD_DAY);
	        //设置执行该任务的用户
	        WTUser administrator = UserUtil.getUserByName("Administrator");
	        
	        scheduleItem.setPrincipalRef(WTPrincipalReference.newWTPrincipalReference(administrator));
	        //将计划任务对象添加到队列
	        scheduleItem = SchedulingHelper.service.addItem(scheduleItem, null);
	        logger.info("计划执行队列创建完毕... ..." + "'启动时间为'" + scheduleItem.getStartDate() + "!" );
		}
    }
    
    public static void main(String[] args) {
    	RemoteMethodServer ser = RemoteMethodServer.getDefault();
    	ser.setUserName("wcadmin");
    	ser.setPassword("tmadmin");
    	try {
			createScheduleQueue();
		} catch (WTException e) {
			e.printStackTrace();
		}
	}

    private static Date addDay(Date today, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(today);
        startDT.add(Calendar.DAY_OF_MONTH, num);
        return startDT.getTime();
    }
}
